# U-Boot: Veyron Chromebooks
# Maintainer: Kevin Mihelich <kevin@archlinuxarm.org>

buildarch=4

pkgbase=uboot-veyron
pkgname=('uboot-minnie' 'uboot-mickey' 'uboot-jerry')
pkgver=2018.07
pkgrel=1
arch=('armv7h')
url="http://git.denx.de/u-boot.git/"
license=('GPL')
makedepends=('git' 'bc' 'dtc' 'vboot-utils' 'python2' 'swig')
conflicts=('linux-veyron' 'linux-armv7-chromebook' 'linux-armv7-rc-chromebook')
install=${pkgbase}.install
source=("ftp://ftp.denx.de/pub/u-boot/u-boot-${pkgver/rc/-rc}.tar.bz2"
        '0001-rk3288-set-CONFIG_SYS_TEXT_BASE-for-depthcharge.patch'
        '0002-rockchip-minnie-Add-a-.its-file-for-chromium.patch'
        '0003-rockchip-mickey-Add-a-.its-file-for-chromium.patch'
        '0004-rockchip-jerry-Revert-.its-changes.patch'
        'boot.txt'
        'mkscr')
md5sums=('2b8eaa30dd118b29889669070da22bb0'
         '95e897b1cecb10cbefd9d3303101f326'
         '1e59cda42c8b50bf7dc133dd38daecdc'
         '7cbca7f7052c352e23c178eb5f97da90'
         '0743c29a757d4299efb0532e651c78ca'
         '029cb473f59f08834b54cdc3e34c9672'
         '021623a04afd29ac3f368977140cfbfd')

prepare() {
  cd u-boot-${pkgver/rc/-rc}
  git apply ../0001-rk3288-set-CONFIG_SYS_TEXT_BASE-for-depthcharge.patch
  git apply ../0002-rockchip-minnie-Add-a-.its-file-for-chromium.patch
  git apply ../0003-rockchip-mickey-Add-a-.its-file-for-chromium.patch
  git apply ../0004-rockchip-jerry-Revert-.its-changes.patch
}

build() {
  cd u-boot-${pkgver/rc/-rc}

  unset LDFLAGS CFLAGS CXXFLAGS
  export PYTHON=/usr/bin/python2

  for i in chromebook_minnie chromebit_mickey chromebook_jerry; do
    msg ${i}
    make O=b/${i} ${i}_defconfig
    echo 'CONFIG_IDENT_STRING=" Arch Linux ARM"' >> b/${i}/.config
    make O=b/${i} EXTRAVERSION=-${pkgrel}
  done
}

common_package() {
  name=$1
  dtb=$2

  cd u-boot-${pkgver/rc/-rc}

  b/${name}/tools/mkimage -f doc/chromium/${name}.its uboot.img
  echo "" > config
  dd if=/dev/zero of=bootloader.bin bs=512 count=1
  vbutil_kernel \
    --pack uboot.kpart \
    --version 1 \
    --vmlinuz uboot.img \
    --arch arm \
    --keyblock doc/chromium/devkeys/kernel.keyblock \
    --signprivate doc/chromium/devkeys/kernel_data_key.vbprivk \
    --config config \
    --bootloader bootloader.bin
  install -Dm644 uboot.kpart "${pkgdir}"/boot/uboot.kpart

  install -Dm644 ../boot.txt "${pkgdir}"/boot/boot.txt
  sed -i "s/@@FDTFILE@@/${dtb}/" "${pkgdir}"/boot/boot.txt
  b/${name}/tools/mkimage -A arm -O linux -T script -C none -n "U-Boot boot script" -d "${pkgdir}"/boot/boot.txt "${pkgdir}"/boot/boot.scr
  install -Dm755 ../mkscr "${pkgdir}"/boot/mkscr
}

package_uboot-minnie() {
  pkgdesc="U-Boot for Veyron Minnie Chromebook"

  common_package "chromebook_minnie" "rk3288-veyron-minnie.dtb"
}

package_uboot-mickey() {
  pkgdesc="U-Boot for Veyron Mickey Chromebit"

  common_package "chromebit_mickey" "rk3288-veyron-mickey.dtb"
}

package_uboot-jerry() {
  pkgdesc="U-Boot for Veyron Jerry Chromebook"

  common_package "chromebook_jerry" "rk3288-veyron-jerry.dtb"
}
